package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.R;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.view.ui.adapter.pick.PickAreaAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.List;

/**
 * 省市同时显示的选择弹窗
 */
public class PickAreaProCityPopWindow extends BasePopWindow {

    private TextView tvTitleFirst;
    private RecyclerView proRecyclerView;
    private TextView tvTitleSecond;
    private RecyclerView cityRecyclerView;
    private TextView tvReset;
    private TextView tvOk;
    private View viewBottom;
    private PickAreaAdapter pickProvinceAdapter;
    private PickAreaAdapter pickCityAdapter;

    private List<PickSelectEntity> provinceDatas;//全部省份数据
    private String proNo = "";//选择的省份的no
    private String proName = "";//选择的省份的名称
    private ProSelectClickCallBack selectProClickCallBack;//选择省份后的回调

    private String cityNo = "";//选择的市的no
    private String cityName = "";//选择的市的名称

    private OKSelectClickCallBack okSelectClickCallBack;//点击确定时的回调

    private int indexPro = 0, indexCity = 0; // 记录省市下标

    public void setDefItem() {
        pickProvinceAdapter.setDefItem(0);
    }


    public PickAreaProCityPopWindow(Context context) {
        super(context);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_pick_procity;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        tvTitleFirst = holder.getPop().findViewById(R.id.tv_title_first);
        proRecyclerView = holder.getPop().findViewById(R.id.rlv_list);
        tvTitleSecond = holder.getPop().findViewById(R.id.tv_title_second);
        cityRecyclerView = holder.getPop().findViewById(R.id.city_list);
        tvReset = holder.getPop().findViewById(R.id.tv_reset);
        tvOk = holder.getPop().findViewById(R.id.tv_ok);
        viewBottom = holder.getPop().findViewById(R.id.view_bottom);


        //省份列表
        pickProvinceAdapter = new PickAreaAdapter(mContext, null, R.layout.item_pick_area_choose_new);
        pickProvinceAdapter.setItemSelect(true);
        pickProvinceAdapter.setDefItem(-1);
        proRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 1));
        proRecyclerView.setAdapter(pickProvinceAdapter);
        pickProvinceAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                proNo = pickSelectEntity.getNo();
                proName = pickSelectEntity.getProjectscale();
                //市区清空
                cityNo = "";
                cityName = "";
                if (position != 0) {
                    indexPro = position;
                    //选择省份后刷新市区列表
                    selectProClickCallBack.refreshCityList(proNo);
                } else {
                    cityNo = "";
                    cityName = "";
                    pickCityAdapter.setDatasMayNull(null);
                }
            }
        });

        //市区列表
        pickCityAdapter = new PickAreaAdapter(mContext, null, R.layout.item_pick_area_child_choose_new);
        pickCityAdapter.setItemSelect(true);
        pickCityAdapter.setDefItem(-1);
        cityRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 1));
        cityRecyclerView.setAdapter(pickCityAdapter);
        pickCityAdapter.setOnItemClick(new AdapterOnItemClick<PickSelectEntity>() {
            @Override
            public void onItemClick(PickSelectEntity pickSelectEntity, int position) {
                cityNo = pickSelectEntity.getNo();
                cityName = pickSelectEntity.getProjectscale();
                indexCity = position;
                //选择市后的回调

            }
        });

        //重置
        tvReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                proNo = null;//全国
                proName = "";
                cityNo = "";
                cityName = "";
                //省市列表更新
                setData(provinceDatas, 0);
                pickCityAdapter.setDatasMayNull(null);

                //重置新修改功能为设置为全国并选定
                dismiss();
                if (null == okSelectClickCallBack) {
                    return;
                }
                if (null == proNo) {//全国的id为null
                    okSelectClickCallBack.selectProCityOK("", proName, cityNo, cityName);
                    return;
                }

            }
        });

        //确定
        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (null == okSelectClickCallBack) {
                    return;
                }
                if (null == proNo) {//全国的id为null
                    okSelectClickCallBack.selectProCityOK("", proName, cityNo, cityName);
                    return;
                }
                okSelectClickCallBack.selectProCityOK(proNo, proName, cityNo, cityName);
            }
        });

        viewBottom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    /**
     * 商品类型复用此Pop，修改UI
     * (不调用此方法时为默认UI显示)
     *
     * @param type
     */
    public void setType(int type) {
        switch (type) {
            case 1:
                tvTitleFirst.setText("商品类型");
                break;
            case 2:
                tvTitleFirst.setText("机械类型");
                break;
            case 3:
                tvTitleFirst.setText("设备类型");
                break;
        }
        tvTitleSecond.setText("类别");
    }

    public void setData(List<PickSelectEntity> provinceDatas) {
        this.provinceDatas = provinceDatas;
        pickProvinceAdapter.setDatas(provinceDatas);
    }

    public void setData(List<PickSelectEntity> provinceDatas, int position) {
        this.provinceDatas = provinceDatas;
        pickProvinceAdapter.setDatas(provinceDatas);
        pickProvinceAdapter.setDefItem(position);
    }

    public void setData(List<PickSelectEntity> provinceDatas, boolean isAll) {
        this.provinceDatas = provinceDatas;
        pickProvinceAdapter.setDatas(provinceDatas);
        if (isAll)
            pickCityAdapter.setDefItem(0);
    }

    public void setCityDatas(List<PickSelectEntity> cityDatas) {
        if (cityDatas != null && cityDatas.size() != 0) {
            pickCityAdapter.setDefItem(-1);
            pickCityAdapter.setDatas(cityDatas);
        } else {
            pickCityAdapter.setDatasMayNull(null);
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (onDisMiss != null)
            onDisMiss.disMiss();
    }

    /**
     * 点选省份后的回调
     */
    public interface ProSelectClickCallBack {
        void refreshCityList(String no);
    }

    public void setSelectProClick(ProSelectClickCallBack selectProClick) {
        this.selectProClickCallBack = selectProClick;
    }

    /**
     * 点击确定时的回调
     */
    public interface OKSelectClickCallBack {
        void selectProCityOK(String proNo, String proName, String cityNo, String cityName);
    }

    public void setOkSelectClickCallBack(OKSelectClickCallBack okSelectClickCallBack) {
        this.okSelectClickCallBack = okSelectClickCallBack;
    }


}
